package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.constant.UserWithdrawalState;
import cn.net.shuxing.yifu.common.mapper.UserBankCardMapper;
import cn.net.shuxing.yifu.common.mapper.UserWithdrawalCashMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserWithdrawalCashService extends BaseService<UserWithdrawalCash, UserWithdrawalCashMapper> {

    @Autowired
    protected UserInfoBindService userInfoBindService;

    @Autowired
    protected UserService userService;

    @Autowired
    protected UserRealInfoService userRealInfoService;

    @Autowired
    protected UserWithdrawalCashMapper userWithdrawalCashMapper;

    @Autowired
    private UserBankCardMapper userBankCardMapper;

    public List<UserWithdrawalCash> getByState(int state) {
        UserWithdrawalCash userWithdrawalCash = new UserWithdrawalCash();
        userWithdrawalCash.clearCreateTimeAndUpdateTime();
        userWithdrawalCash.setState(state);
        return gets(userWithdrawalCash);
    }

    public List<UserWithdrawalCash> getsByUserType(Integer userType) {
        List<User> userList = userService.getsByUserType(userType);
        ArrayList<UserWithdrawalCash> list = new ArrayList<>();
        for (User user : userList) {
            UserWithdrawalCash userWithdrawalCash = new UserWithdrawalCash();
            userWithdrawalCash.clearCreateTimeAndUpdateTime();
            userWithdrawalCash.setUserId(user.getId());
//            list.add(get(userWithdrawalCash));
            List<UserWithdrawalCash> userWithdrawalCashList = getsByUserId(user.getId());
            list.addAll(userWithdrawalCashList);
        }
        return list;
    }

    public BigDecimal getWithdrawalCashCountByUserType(Integer userType) {
        List<UserWithdrawalCash> userWithdrawalCashes = getsByUserType(userType);
        BigDecimal withdrawalCashCount = new BigDecimal(0);
        for (UserWithdrawalCash userWithdrawalCash : userWithdrawalCashes) {
            BigDecimal applyMoney = new BigDecimal(userWithdrawalCash.getApplyMoney()).divide(new BigDecimal(100), 8, BigDecimal.ROUND_HALF_UP);
            withdrawalCashCount = withdrawalCashCount.add(applyMoney);
        }
        return withdrawalCashCount;
    }

    @Override
    protected void complete(UserWithdrawalCash bean) {
        if (bean == null)
            return;
        Integer userId = bean.getUserId();
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        String realName = "";
        if (userRealInfo != null)
            realName = userRealInfo.getRealName();
        bean.setUserName(realName);
        UserInfoBind userInfoBind = userInfoBindService.getByPrimaryKey(userId);
        String phoneNum = "";
        if (userInfoBind != null)
            phoneNum = userInfoBind.getPhoneNum();
        bean.setPhoneNum(phoneNum);
        Integer type = null;
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        if (user != null) {
            type = user.getType();
        }

        String userType = "";
        if (type == UserType.SHAREHOLDER) {
            userType = "股东";
        } else if (type == UserType.MEMBER) {
            userType = "会员";
        }
        bean.setUserType(userType);

        String stateName = "";
//        审核状态：0 申请中 1 审核通过等待打款  2：审核未通过 3: 提现完成
        if (bean.getState() == 0) {
            stateName = "申请中";
        } else if (bean.getState() == 1) {
            stateName = "等待打款";
        } else if (bean.getState() == 2) {
            stateName = "提现完成";
        } else if (bean.getState() == 3) {
            stateName = "申请拒绝";
        }
        bean.setStateName(stateName);
    }

    public List<UserWithdrawalCash> getsByUserId(Integer userId) {
        UserWithdrawalCash userWithdrawalCash = new UserWithdrawalCash();
        userWithdrawalCash.clearCreateTimeAndUpdateTime();
        userWithdrawalCash.setUserId(userId);
//            list.add(get(userWithdrawalCash));
        List<UserWithdrawalCash> userWithdrawalCashList = gets(userWithdrawalCash);
        for (UserWithdrawalCash withdrawalCash : userWithdrawalCashList) {
            String applyBankCardNum = withdrawalCash.getApplyBankCardNum();
            applyBankCardNum = applyBankCardNum.substring(0, 4) + "****" + applyBankCardNum.substring(applyBankCardNum.length() - 4);
            withdrawalCash.setApplyBankCardNum(applyBankCardNum);
        }
        return userWithdrawalCashList;
    }

    /**
     * 获取服务端人员提现列表
     *
     * @param userId
     * @param type
     * @return
     */
    public List<UserWithdrawalCash> getWithdrawalList(Integer startPageOn, Integer pageSize, Integer userId, Integer type) {
        List<UserWithdrawalCash> userWithdrawalCashList = userWithdrawalCashMapper.getWithdrawalList(startPageOn, pageSize, userId, type);
        if (null != userWithdrawalCashList && userWithdrawalCashList.size() > 0) {
            for (UserWithdrawalCash userWithdraw : userWithdrawalCashList) {
                UserBankCard userBankCard = userBankCardMapper.selectUserBankCard(2, userWithdraw.getUserId());
                if (null != userBankCard) {
                    userWithdraw.setUserName(userBankCard.getHolderName());
                }
                if (null != userWithdraw.getState()) {
//                    0 申请中 1 等待打款  2：提现完成 3: 申请拒绝
                    if (userWithdraw.getState() == 0) {
                        userWithdraw.setStateName("申请中");
                    } else if (userWithdraw.getState() == 1) {
                        userWithdraw.setStateName("等待打款");
                    } else if (userWithdraw.getState() == 2) {
                        userWithdraw.setStateName("提现完成");
                    } else {
                        userWithdraw.setStateName("申请拒绝");
                    }
                }
                userWithdraw.setFactMoney(Integer.valueOf(new BigDecimal(userWithdraw.getFactMoney()).divide(new BigDecimal(100)).toString()));
            }
        }
        return userWithdrawalCashList;
    }

    public List<UserWithdrawalCash> getsByUserIdAndState(Integer userId, Integer state) {
        CommonExample example = new CommonExample(UserWithdrawalCash.class);
        example.equals("userId", userId);
        example.equals("state", state);
        return getsByExampleWithoutComplete(example);
    }

    public List<UserWithdrawalCash> getsCompleted(Integer state) {
        CommonExample commonExample = new CommonExample(UserWithdrawalCash.class);
        commonExample.equals("state", state);
        return getsByExampleWithoutComplete(commonExample);
    }

}
